package tech.aistar.corejava.day14;

import java.io.*;

/**
 * Created by 豆梦寒 on 2019/8/2.
 */
public class ByteIODemo {
    public static void main(String[] args) {
        copyFile("D:/测试3.txt","D:/测试3_副本.txt");
    }

    public static void copyFile(String src,String target){
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(target);

            //定义一个缓存区数组
            byte[] buf = new byte[3*1024];//3KB
            //循环读取
            while(true){
                //in.read(buf) - 从输入流读取的字节存储到buf字节数组中.
                //len - 代表的是读取的字节的大小.
                int len = in.read(buf);

                if(len == -1)
                    break;
                //System.out.println(len);

                //将buf写出去,从0位置取完写出去len个字节
                out.write(buf,0,len);

                System.out.println("文件拷贝!");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
